﻿using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Text;

namespace Xfm.TagHelpers.Exts
{
    public static class TagHelperExtension
    {
        private const string cls = "class";
        /// <summary>
        /// 添加class,不影响原始class
        /// </summary>
        /// <param name="output"></param>
        /// <param name="vals"></param>
        /// <returns></returns>
        public static TagHelperOutput AddClass(this TagHelperOutput output, string vals)
        {
            if (output == null)
                throw new ArgumentNullException(nameof(output));
            if (output.Attributes.TryGetAttribute(cls, out TagHelperAttribute classElement))
            {
                output.Attributes.SetAttribute(cls, $"{classElement.Value.ToString()} {vals}");
            }
            else
            {
                output.Attributes.Add(cls, vals);
            }
            return output;
        }
        /// <summary>
        /// 覆盖class
        /// </summary>
        /// <param name="output"></param>
        /// <param name="vals"></param>
        /// <returns></returns>
        public static TagHelperOutput SetClass(this TagHelperOutput output, string vals)
        {
            if (output == null)
                throw new ArgumentNullException(nameof(output));
            output.Attributes.SetAttribute(cls, vals);
            return output;
        }

    }
}
